//
// Created by LiuYou on 2021/10/26.
//

#include <iostream>


static FILE* fp;


int main(int argc, char** argv) {
    if (argc < 2) {
        fprintf( stderr, "用法: %s file\n", argv[0] );
        return 0;
    }


    fp = fopen( argv[1], "r" );
    if (fp == nullptr) {
        perror( "fopen()" );
        return 1;
    }
    atexit( [] {
        fclose( fp );
    } );

//    int fileSize{};
//    while (true) {
//        int readChar = fgetc( fp );
//        if (readChar == EOF) {
//            break;
//        }
//        ++fileSize;
//    }

    fseek( fp, 0, SEEK_END );
    long fileSize = ftell( fp );


    printf( "当前文件的大小为: %ld\n", fileSize );
    printf( "sizeof(long) = %lu\n", sizeof( long ));
    // long 是 8 个字节。


    return 0;
}
/*
liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2/src/io$ ./a.out testTmp
当前文件的大小为: 14464
liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2/src/io$ ll
总用量 92
drwxrwxr-x 2 liuyou liuyou  4096 10月 26 14:35 ./
drwxrwxr-x 5 liuyou liuyou  4096 10月 26 10:40 ../
-rwxrwxr-x 1 liuyou liuyou 17592 10月 26 14:35 a.out*
-rw-rw-r-- 1 liuyou liuyou 26636 10月 26 14:30 CMakeLists.txt
-rw-rw-r-- 1 liuyou liuyou   658 10月 26 14:35 IO-CalculateFileSize1.cpp
-rw-rw-r-- 1 liuyou liuyou   874 10月 26 13:38 IO-fopen1.cpp
-rw-rw-r-- 1 liuyou liuyou   782 10月 26 13:39 IO-fopen2.cpp
-rw-rw-r-- 1 liuyou liuyou  3845 10月 26 14:02 IO-fopen3.cpp
-rw-rw-r-- 1 liuyou liuyou   867 10月 26 14:25 IO-MyCopy1.cpp
-rw-rw-r-- 1 liuyou liuyou 14464 10月 26 14:18 testTmp
liuyou@liuyou-virtual-machine:~/CLionProjects/LinuxEnvironmentProgramming2/src/io$

 */
